class PageInfo(object):

    def __init__(self, current_page=None, all_count=None, url=None, per_page=20, show_page=11):
        try:
            if not per_page:
                per_page = 20
            if int(current_page) > 0:
                self.current_page = int(current_page)
            else:
                self.current_page = 1
        except Exception as e:
            self.current_page = 1
        self.per_page = int(per_page)
        a, b = divmod(all_count, self.per_page)
        if b:
            a = a + 1
        self.all_pager = a
        self.show_page = show_page
        self.url = url

    def start(self):
        return (self.current_page-1) * self.per_page

    def end(self):
        return self.current_page * self.per_page

    def pager(self):
        page_list = []
        half = int((self.show_page-1)/2)
        if self.all_pager < self.show_page:
            begin = 1
            stop = self.all_pager + 1
        else:
            if self.current_page <= half:
                begin = 1
                stop = self.show_page + 1
            else:
                if self.current_page + half > self.all_pager:
                    begin = self.all_pager - self.show_page + 1
                    stop = self.all_pager + 1
                else:
                    begin = self.current_page - half
                    stop = self.current_page + half + 1
        home_page = "<li><a href='%s?page=%s'>首页</a></li>" % (self.url, 1)
        page_list.append(home_page)
        if self.current_page <= 1:
            prev = "<li><a href='#'>上一页</a></li>"
        else:
            prev = "<li><a href='%s?page=%s'>上一页</a></li>" % (self.url, self.current_page - 1)
        page_list.append(prev)
        for i in range(begin, stop):
            if i == self.current_page:
                temp = "<li class='active'><a href='%s?page=%s'>%s</a></li>" % (self.url, i, i)
            else:
                temp = "<li><a href='%s?page=%s'>%s</a></li>" % (self.url, i, i)

            page_list.append(temp)
        if self.current_page >= self.all_pager:
            nex = "<li><a href='#'>下一页</a></li>"
        else:
            nex = "<li><a href='%s?page=%s'>下一页</a></li>" % (self.url, self.current_page + 1)
        page_list.append(nex)
        trailing_page = "<li><a href='%s?page=%s'>尾页</a></li>" % (self.url, self.all_pager)
        page_list.append(trailing_page)
        return ''.join(page_list)
